﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;

namespace CodeFarmer.Controller
{
    /// <summary>
    /// 验证码 ActionResult
    /// </summary>
    public class AuthenCode : ActionResult
    {
        /// <summary>
        /// 背景色
        /// </summary>
        public Color BackGroundColor { get; set; }
        /// <summary>
        /// 文本颜色
        /// </summary>
        public Color RandomTextColor { get; set; }
        /// <summary>
        /// 文本内容
        /// </summary>
        public string RandomText { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            RenderAuthenImage(context);
        }

        /// <summary>
        /// 绘制验证码
        /// </summary>
        /// <param name="context">控制器上下文</param>
        private void RenderAuthenImage(ControllerContext context)
        {
            int width = (int)(RandomText.Length * 17);
            Bitmap image = new Bitmap(width, 23);
            Graphics g = Graphics.FromImage(image);
            Font f = null;
            String textFont, text;

            String[] crypticFonts = new String[11];
            crypticFonts[0] = "Arial";
            crypticFonts[1] = "Verdana";
            crypticFonts[2] = "Comic Sans MS";
            crypticFonts[3] = "Impact";
            crypticFonts[4] = "Haettenschweiler";
            crypticFonts[5] = "Lucida Sans Unicode";
            crypticFonts[6] = "Garamond";
            crypticFonts[7] = "Courier New";
            crypticFonts[8] = "Book Antiqua";
            crypticFonts[9] = "Arial Narrow";
            crypticFonts[10] = "Estrangelo Edessa";

            //背景色
            g.Clear(BackGroundColor);
            //前景色
            Brush b = new SolidBrush(RandomTextColor);
            //填充文字
            for (int i = 0; i <= RandomText.Length - 1; i++)
            {
                textFont = crypticFonts[new Random().Next(i)];
                f = new Font(textFont, 12, FontStyle.Bold | FontStyle.Italic);
                text = RandomText.Substring(i, 1);
                g.DrawString(text, f, b, i * 15, 1);
            }
            // 随机线条        
            Pen linePen = new Pen(Color.Gray, 0);
            Random rand = new Random();
            for (int i = 0; i < 3; i++)
            {
                int x1 = rand.Next(image.Width);
                int y1 = rand.Next(image.Height);
                int x2 = rand.Next(image.Width);
                int y2 = rand.Next(image.Height);
                g.DrawLine(linePen, x1, y1, x2, y2);
            }
            // 随机点      
            for (int i = 0; i < 30; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                image.SetPixel(x, y, Color.Gray);
            }
            // 边框      
            g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
            //输出图片
            context.HttpContext.Response.ContentType = "image/gif";
            image.Save(context.HttpContext.Response.OutputStream, ImageFormat.Gif);
            f.Dispose();
            g.Dispose();
            image.Dispose();
        }
    }
}
